Load packages
library(tidyverse)
library(tidytext)
library(igraph)
library(ggraph)
library(stringr)
library(widyr)
library(knitr)
library(topicmodels)
Get data.
dat <- read_csv("data/aggregated_data.csv") %>%
filter(!is.na(title))
Make a data frame that lists all the authors in separate columns for each paper.
author_df <- dat %>% select(id, authors) %>%
unnest_tokens(output = authors_long, input = authors, token = stringr::str_split, pattern = ";")
Convert each author name to “last, first initial” format.
author_df <- author_df %>%
mutate(authors_long = str_trim(authors_long)) %>%
mutate(last = ifelse(grepl(",", authors_long) == TRUE,
str_extract(authors_long, "[^,]*"),
str_extract(authors_long, "[^ ]*$"))) %>%
mutate(first_init = ifelse(grepl(",", authors_long) == TRUE,
strsplit(authors_long, " "),
str_sub(authors_long, start = 1, end = 1))) %>%
unnest(first_init) %>%
group_by(authors_long, id) %>%
slice(2) %>%
ungroup() %>%
arrange(id) %>%
mutate(first_init = str_sub(first_init, 1, 1)) %>%
mutate(author = paste0(first_init, ". ", last)) %>%
select(-last, -first_init)
kable(head(author_df))
| id | authors_long | author |
|---|---|---|
| 17 | cannon, ellie | e. cannon |
| 17 | copenhaver-parry, paige e. | p. copenhaver-parry |
| 18 | betts, matthew g. | m. betts |
| 18 | frey, sarah j. k. | s. frey |
| 18 | hadley, adam s. | a. hadley |
| 20 | barnhart, theodore b. | t. barnhart |
write_csv(author_df, "data/authors.csv")
Now column “author” contains the most standard version. It looks like we have 1966 unique authors.
Get pairwise author count:
author_pairs <- author_df %>%
pairwise_count(author, id, sort = TRUE, upper = FALSE)
names(author_pairs)[1:2] <- c("author1", "author2")
kable(head(author_pairs))
| author1 | author2 | n |
|---|---|---|
| w. romme | m. turner | 9 |
| j. bradford | w. lauenroth | 6 |
| a. hamlet | d. lettenmaier | 6 |
| l. leung | y. qian | 6 |
| d. horan | d. isaak | 5 |
| d. isaak | d. nagel | 5 |
Make a yearly version of author pairs as well:
author_df <- full_join(author_df, dat[, c("id", "year")], by = "id")
author_df <- author_df %>% filter(!is.na(year))
years <- unique(author_df$year)
pair_dfs <- vector("list", length(years))
for(i in 1:length(years)){
df <- author_df %>% filter(year == years[i])
pair_dfs[[i]] <- df %>%
pairwise_count(author, id, sort = TRUE, upper = FALSE) %>%
mutate(year = years[i])
}
author_pairs_years <- bind_rows(pair_dfs)
names(author_pairs_years)[1:2] <- c("author1", "author2")
#Subset to years with > 50 author collaborations in our dataset.
#(save a copy with the full dataset for later).
apy_full <- author_pairs_years
years <- author_pairs_years %>% group_by(year) %>% count() %>% filter(nn > 50)
author_pairs_years <- author_pairs_years %>% filter(year %in% years$year)
Join authors with subject data (using journal, discipline, keyword?). A possible approach here is to do some topic modeling with the keywords (276 papers are missing keyword data), categorize papers by topic, and then do network analysis grouped by topic. This is probably a good idea anyway because we want to split papers by topic for when we code for content.
keyword_df <- dat %>%
dplyr::select(id, keywords) %>%
mutate(keywords = gsub(",", ";", keywords)) %>%
unnest_tokens(input = keywords, output = keywords, token = stringr::str_split, pattern = ";") %>%
mutate(keywords = str_trim(keywords)) %>%
filter(!is.na(keywords))
We’ve got 2136 unique keywords. Let’s look at keyword pairs to see how they’re grouped.
keyword_pairs <- keyword_df %>%
pairwise_count(keywords, id, sort = TRUE, upper = FALSE)
set.seed(1234)
keyword_pairs %>%
filter(n >= 10) %>%
graph_from_data_frame() %>%
ggraph(layout = "fr") +
geom_edge_link(aes(edge_alpha = n, edge_width = n), edge_colour = "salmon") +
geom_node_point(size = 2) +
geom_node_text(aes(label = name), repel = TRUE,
point.padding = unit(0.2, "lines")) +
theme_void()
Wow, that keyword network seems to be very centralized. And I worry a little bit that the structure we see here is just an artifact of how people happen to decide on topics.
keyword_cors <- keyword_df %>%
group_by(keywords) %>%
#filter(n() >= 50) %>%
pairwise_cor(keywords, id, sort = TRUE, upper = FALSE)
#Take out keyword correlations with correlation 1; these are redundant.
keyword_cors <- keyword_cors %>%
filter(round(correlation, 3) < 1)
kable(head(keyword_cors))
| item1 | item2 | correlation |
|---|---|---|
| plant pathogenic fungi | plant pathogens | 0.9347054 |
| fungal diseases | plant pathogenic fungi | 0.9251195 |
| plant pests | insect pests | 0.9135562 |
| aquatic plants | phytoplankton | 0.9121812 |
| air pollutants | air pollution | 0.9121812 |
| arthropod pests | pests | 0.9031583 |
Visualize the network of keyword correlations:
set.seed(1234)
keyword_cors %>%
filter(correlation > .7) %>%
graph_from_data_frame() %>%
ggraph(layout = "kk") +
geom_edge_link(aes(edge_alpha = correlation, edge_width = correlation), edge_colour = "darkorchid") +
geom_node_point(size = 2) +
geom_node_text(aes(label = name), repel = FALSE,
#point.padding = unit(0.2, "lines"),
size = 2) +
theme_void()
Well, that’s extremely difficult to read, but it does look like there’s some more meaningful grouping with correlations than there was with just pairwise analysis.
Let’s move on to some topic modeling to see if we can group papers. First, define stop words in addition to the common ones, and get word counts.
my_stop_words <- data_frame(word = c("climate change", "usa"),
lexicon = rep("custom", 2))
word_counts <- keyword_df %>%
rename(word = keywords) %>%
anti_join(my_stop_words) %>%
count(id, word, sort = TRUE) %>%
ungroup() %>%
arrange(-n)
## Joining, by = "word"
word_counts
## # A tibble: 6,461 x 3
## id word n
## <int> <chr> <int>
## 1 1194 dissolved organic 2
## 2 17 bayesian model 1
## 3 17 distribution edge 1
## 4 17 distribution shift 1
## 5 17 plant performance 1
## 6 17 sensitivity analysis 1
## 7 18 composition 1
## 8 18 dynamic occupancy models 1
## 9 18 forest bird distributions 1
## 10 18 forest structure and 1
## # ... with 6,451 more rows
This topic modeling approach may not make a lot of sense since keywords generally only appear once… but continue anyway.
keyword_dtm <- word_counts %>%
cast_dtm(id, word, n)
keyword_lda <- LDA(keyword_dtm, k = 8, control = list(seed = 1234))
tidy_lda <- tidy(keyword_lda)
top_terms <- tidy_lda %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta)
top_terms %>%
mutate(term = reorder(term, beta)) %>%
group_by(topic, term) %>%
arrange(desc(beta)) %>%
ungroup() %>%
mutate(term = factor(paste(term, topic, sep = "__"),
levels = rev(paste(term, topic, sep = "__")))) %>%
ggplot(aes(term, beta, fill = as.factor(topic))) +
geom_col(show.legend = FALSE) +
coord_flip() +
scale_x_discrete(labels = function(x) gsub("__.+$", "", x)) +
labs(title = "Top 10 terms in each LDA topic",
x = NULL, y = expression(beta)) +
facet_wrap(~ topic, ncol = 4, scales = "free")
These look like they’re somewhat informative, but imperfect. They would probably be more informative if we did this same procedure with full texts or abstracts (which might be a good next step, using crminer).
For now, can we categorize each paper by its topic?
lda_gamma <- tidy(keyword_lda, matrix = "gamma")
id_topic <- lda_gamma %>%
group_by(document) %>%
filter(gamma == max(gamma)) %>%
ungroup() %>%
select(document, topic) %>%
rename(id = document) %>%
mutate(id = as.integer(id))
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
network_df <- left_join(author_df, id_topic, by = "id")
author_topics <- network_df %>%
group_by(author) %>%
summarise(topic_new = Mode(topic))
kable(head(author_topics))
| author | topic_new |
|---|---|
| a. adams | 5 |
| a. adeloye | 1 |
| a. ager | 5 |
| a. aldous | 6 |
| a. andrade | 5 |
| a. armstrong | 7 |
#author_pairs <- author_df %>%
# pairwise_count(author, id, sort = TRUE, upper = FALSE)
#names(author_pairs)[1:2] <- c("author1", "author2")
#kable(head(author_pairs))
abstract_df <- dat %>% select(id, abstract) %>%
filter(!is.na(abstract)) %>%
unnest_tokens(word, abstract) %>%
anti_join(stop_words)
## Joining, by = "word"
abstract_df %>% count(word, sort = TRUE)
## # A tibble: 9,977 x 2
## word n
## <chr> <int>
## 1 climate 2184
## 2 change 1161
## 3 species 836
## 4 temperature 767
## 5 precipitation 698
## 6 water 691
## 7 model 642
## 8 forest 610
## 9 1999 583
## 10 fire 564
## # ... with 9,967 more rows
#Looks like there are some numbers... maybe get rid of those.
#Also, change everything to lower case.
abstract_df <- abstract_df %>%
filter(!grepl("[[:digit:]]", word)) %>%
mutate(word = tolower(word))
#Need to add a few more stop words:
abstract_df <- abstract_df %>%
filter(!word %in% c("org", "http", "xhtml", "xmlns", "climate"))
library(widyr)
abstract_word_pairs <- abstract_df %>%
pairwise_count(word, id, sort = TRUE, upper = FALSE)
#this is slow - only run if needed.
# abstract_cors <- abstract_df %>%
# group_by(word) %>%
# #filter(n() >= 50) %>%
# pairwise_cor(word, id, sort = TRUE, upper = FALSE)
#Calculate tf_idf.
abstract_tf_idf <- abstract_df %>%
count(id, word, sort = TRUE) %>%
ungroup() %>%
bind_tf_idf(word, id, n)
word_counts <- abstract_df %>%
count(id, word, sort = TRUE) %>%
ungroup()
word_counts
## # A tibble: 79,097 x 3
## id word n
## <int> <chr> <int>
## 1 2375 glacier 22
## 2 2444 glacier 22
## 3 615 degrees 19
## 4 2475 aspen 18
## 5 2875 species 18
## 6 3078 water 18
## 7 99 recharge 17
## 8 194 species 17
## 9 700 stream 17
## 10 821 snow 17
## # ... with 79,087 more rows
abstract_dtm <- word_counts %>%
cast_dtm(id, word, n)
#This may take a while.
abstract_lda <- LDA(abstract_dtm, k = 8, control = list(seed = 1234))
tidy_lda <- tidy(abstract_lda)
top_terms <- tidy_lda %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta)
top_terms
## # A tibble: 80 x 3
## topic term beta
## <int> <chr> <dbl>
## 1 1 fire 0.033224806
## 2 1 forest 0.017407021
## 3 1 carbon 0.011841713
## 4 1 forests 0.010755105
## 5 1 soil 0.009741968
## 6 1 severity 0.008870568
## 7 1 burned 0.008357221
## 8 1 disturbance 0.007976146
## 9 1 wildfire 0.007410203
## 10 1 fires 0.007398807
## # ... with 70 more rows
top_terms %>%
mutate(term = reorder(term, beta)) %>%
group_by(topic, term) %>%
arrange(desc(beta)) %>%
ungroup() %>%
mutate(term = factor(paste(term, topic, sep = "__"),
levels = rev(paste(term, topic, sep = "__")))) %>%
ggplot(aes(term, beta, fill = as.factor(topic))) +
geom_col(show.legend = FALSE) +
coord_flip() +
scale_x_discrete(labels = function(x) gsub("__.+$", "", x)) +
labs(title = "Top 10 terms in each LDA topic",
x = NULL, y = expression(beta)) +
facet_wrap(~ topic, ncol = 4, scales = "free")
lda_gamma <- tidy(abstract_lda, matrix = "gamma")
ggplot(lda_gamma, aes(gamma, fill = as.factor(topic))) +
geom_histogram(show.legend = FALSE, bins = 40) +
facet_wrap(~ topic, ncol = 4) +
scale_y_log10() +
labs(title = "Distribution of probability for each topic",
y = "Number of documents", x = expression(gamma))
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 7 rows containing missing values (geom_bar).
It doesn’t look like these are very well separated: in an ideal world where each document fits perfectly into one category, we would just have bars at the far left and right sides of each facet.
Repeat the topic-modeling with term frequency-inverse document frequency (TF-IDF) instead of word counts:
## # A tibble: 80 x 3
## topic term beta
## <int> <chr> <dbl>
## 1 1 fire 0.011607530
## 2 1 forest 0.005911603
## 3 1 stand 0.005449802
## 4 1 severity 0.005425651
## 5 1 burned 0.005305420
## 6 1 fires 0.005079615
## 7 1 aspen 0.004897655
## 8 1 carbon 0.004886524
## 9 1 beetle 0.004877169
## 10 1 wildfire 0.004625715
## # ... with 70 more rows
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 11 rows containing missing values (geom_bar).
I’ve played around a little with different numbers of topics, and I don’t think the gammas are a lot better with fewer than more topics - although, selecting only four topics (for example) makes the topics a lot more intelligible to me.
Take a look at how the most common terms in abstracts have changed over time.
#Add year to abstract word counts, and count number of times each word occurs in each year.
df <- full_join(abstract_df, author_df[,c("id", "year")], by = "id") %>%
group_by(year, word) %>%
count() %>%
arrange(year, desc(n))
#Figure out most common terms and see how they've changed over time (could also choose terms of interest in another way):
top_words <- df %>%
group_by(word) %>%
summarise(total = sum(n)) %>%
arrange(desc(total))
top <- top_words[1:10,]
plot_dat <- df %>% filter(word %in% top$word) %>%
filter(year < 2017) %>%
filter(year > 1980)
ggplot(plot_dat, aes(x = year, y = n, group = word, color = word)) +
geom_line()
If we’re interested in doing more with this, two good next steps would be to normalize the data by total number of words in the corpus for each year (the corpus gets bigger over time, so words naturally become more frequent) and to think more about what words we’re actually interested in. It might be that combinations of words are really the most interesting (bark beetle? forest carbon? stream temperature?).